package com.xoms.pad.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.pad.aspect.datasource.DataSourceSwitcher;
import com.xoms.pad.controller.res.bean.RDimDetailBean;
import com.xoms.pad.controller.res.bean.RDimensionBean;
import com.xoms.pad.model.DimDetailBean;
import com.xoms.pad.model.DimensionBean;
import com.xoms.pad.persist.dao.DimDetailDao;
import com.xoms.pad.persist.impl.DimDetailImpl;
import com.xoms.pad.persist.impl.DimensionImpl;
import com.xoms.pad.service.DimensionService;

@Service
public class DimensionServiceImpl implements DimensionService {

	
	@Autowired
	DimDetailImpl dimDetialImpl;
	
	@Autowired
	DimDetailDao dimDetailDao;
	
	@Autowired
	DimensionImpl dimensionImpl;
	

	@Override
	public List<RDimensionBean> getDimension(String did) {
		
				//获取数据源
				DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_XOMS);
				//通过did去维度信息
				List<DimensionBean> dimensions = new  ArrayList<>();
					dimensions = dimensionImpl.selectDimension(did);
					if (dimensions == null || dimensions.size()<=0) {
						return null;
					}
				
				List<RDimensionBean> rDimBean = new ArrayList<RDimensionBean>();
				RDimensionBean rDimensionBean;
				for(DimensionBean dimensionBean:dimensions){
					rDimensionBean = new RDimensionBean();
					rDimensionBean.setDid(dimensionBean.getDid());
					rDimensionBean.setName(dimensionBean.getName());
					rDimensionBean.setRemark(dimensionBean.getRemark());
					rDimensionBean.setStatus(dimensionBean.getStatus());
					
					String dids = dimensionBean.getDid(); //获取维度ID
					//获取数据源
					DataSourceSwitcher.setDataSourceType(DataSourceSwitcher.DATA_SOURCE_XOMS);
					List<DimDetailBean> dimDetail = dimDetailDao.selectDimDetail(dids);
					List<RDimDetailBean> dimDetailList = new ArrayList<>();
					for(DimDetailBean dimDetaiBean: dimDetail){
						RDimDetailBean rDimDetailBean = new RDimDetailBean();
						rDimDetailBean.setDid(dimDetaiBean.getDid());
						rDimDetailBean.setDimensionId(dimDetaiBean.getDimensionId());
						rDimDetailBean.setName(dimDetaiBean.getName());
						rDimDetailBean.setRemark(dimDetaiBean.getRemark());
						rDimDetailBean.setStatus(dimDetaiBean.getStatus());
						rDimDetailBean.setCreateTime(dimDetaiBean.getCreateTime());
						rDimDetailBean.setAccountId(dimDetaiBean.getAccountId());
						
						dimDetailList.add(rDimDetailBean);
					}
					rDimensionBean.setDimDetail(dimDetailList);
					
					rDimBean.add(rDimensionBean);
				}
				
		return rDimBean;
	}




	


}
